package com.wy.panda.config;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wy.panda.common.property.converter.ObjectToEnumConverter;
import com.wy.panda.interceptor.ReqDurationStatisticsInterceptor;
import com.wy.panda.interceptor.ValidateSignInterceptor;

/**
 * spring mvc配置
 *
 * @author WangYuan
 * @since 2020年7月29日 上午9:24:37
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	@Autowired
	private ReqDurationStatisticsInterceptor reqDurationStatisticsInterceptor;
	
	@Autowired
	private ValidateSignInterceptor validateSignInterceptor;

	@Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        WebMvcConfigurer.super.configureMessageConverters(converters);

        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = null;
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        HttpMessageConverter<?> converter = null;
        while(iterator.hasNext()) {
            converter = iterator.next();
            if(converter instanceof MappingJackson2HttpMessageConverter) {
                mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) converter;
                break;
            }
        }

        if(mappingJackson2HttpMessageConverter == null) {
            Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json();
            builder.indentOutput(true);
            builder.serializationInclusion(Include.NON_NULL);
        } else {
            ObjectMapper objectMapper = mappingJackson2HttpMessageConverter.getObjectMapper();
            objectMapper.setSerializationInclusion(Include.NON_NULL);
        }
    }

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		InterceptorRegistration registration = registry.addInterceptor(reqDurationStatisticsInterceptor);
		registration.addPathPatterns("/**");
		
		InterceptorRegistration validateSignRegistration = registry.addInterceptor(validateSignInterceptor);
		validateSignRegistration.excludePathPatterns("/**");
	}

    @Override
    public void addFormatters(FormatterRegistry registry) {
        WebMvcConfigurer.super.addFormatters(registry);
        
        registry.addConverter(new ObjectToEnumConverter());
    }
}

